﻿#ifndef MAINFORM_H
#define MAINFORM_H

#include <QWidget>
#include <Common/common.h>
#include <SqliteUtil/sqliteutil.h>
#include <Message/message.h>
#include <QMutex>
#include <QMap>
#include <cameracontroller.h>
#include <Thread/camerathread.h>
#include <Thread/saveimgthread.h>
#include <Alg/wooddetectprocessor.h>
#include <From/saveform.h>
#include <From/dataform.h>

namespace Ui {
class MainForm;
}

class MainForm : public QWidget
{
    Q_OBJECT

public:
    explicit MainForm(QWidget *parent = nullptr);
    ~MainForm();

    void initializePage();
    void initializeData();
    void initCameras();

    // 创建相机线程
    void createCameraThread(IMV_DeviceList m_deviceInfoList);
    void releaseAllCameras();

    void processWoodImages();

    void CreateCameraSaveFile();

    int getCameraID(const QString& cameraKey);

    void stopAll();
    void drawRectangle(cv::Mat& image, const cv::Rect& rect, cv::Scalar color = cv::Scalar(0, 255, 0), int thickness = 2);
    void drawRectangle(cv::Mat& image, cv::Point pt1, cv::Point pt2, cv::Scalar color = cv::Scalar(0, 255, 0), int thickness = 2);
    void drawTextOnImage(cv::Mat& image,
                         const std::vector<std::string>& texts,
                         cv::Point startPos = cv::Point(10, 20),
                         int fontFace = cv::FONT_HERSHEY_SIMPLEX,
                         double fontScale = 0.6,
                         cv::Scalar color = cv::Scalar(0, 255, 0),
                         int thickness = 2,
                         int lineSpacing = 25);

    double pixelsToInches(int pixels, int dpi);

    double pixelsToMm(int pixels, int dpi);


    int classifyWoodGrade(unsigned int** id, unsigned int* len);

    double calculateGoodWoodRatio(unsigned int** id, unsigned int* len);

    void assignDefectConfidenceToStruct(
        T_BatchLine& line,
        unsigned int** id,
        float** confidence,
        unsigned int* len
        );

    void calculateWoodDimensionsFromRects(
        const std::vector<cv::Rect>& woodRects,
        T_BatchLine& line,
        double dpi = 96.0
        );

    void generateWoodInspectionReport(Detected_Data data);
    double roundToTwoDecimalPlaces(double value);

signals:
    void sigAlgTestResult(QImage result);

    void sigToMotor(int sig);
public  slots:
    void handleFrame(CFrameInfo frameInfo);
    void woodFrame(CFrameInfo frameInfo);
    void handleError(QString message);
    void handleAlgorithmResult(int cameraId, const QImage &result);
    void checkAlgorithmResults();
    void updateSaveStates(int index);

    void slotAlgTestPath(const QFileInfoList& fileList);

    void slotBatcnInfo(int batchId);
private slots:
    void on_Bt_start_clicked();

    void on_Bt_Save_clicked();

    void on_Bt_CreatBatch_clicked();

private:
    Ui::MainForm *ui;
    int m_nStart;

    IMV_DeviceList m_deviceInfoList;	// 发现的相机列表 | List of cameras found

    QMap<QString, CameraThread*> cameraThreads;

private:
    WoodDetectProcessor* m_woodProcessor;
    QMap<QString, CFrameInfo> m_currentFrames;
private:
    // 添加面阵相机ROI区域
    QRect m_areaRoi5;
    QRect m_areaRoi6;

    // 添加ROI配置读取函数
    void readAreaCameraConfig();
private:
    std::vector<cv::Mat> m_vListC1;
    std::vector<cv::Mat> m_vListC2;
    std::vector<cv::Mat> m_vListC3;
    std::vector<cv::Mat> m_vListC4;

    CameraThread* m_tCamera1;
    CameraThread* m_tCamera2;
    CameraThread* m_tCamera3;
    CameraThread* m_tCamera4;
    CameraThread* m_tCamera5;
    CameraThread* m_tCamera6;

    SaveImgThread* m_tSaveImg1;
    SaveImgThread* m_tSaveImg2;
    SaveImgThread* m_tSaveImg3;
    SaveImgThread* m_tSaveImg4;
    SaveImgThread* m_tSaveImg5;
    SaveImgThread* m_tSaveImg6;

    SaveForm* m_SaveForm;

    qint64 m_captureTime = 0; // 抓取开始
    qint64 m_processStartTime = 0;  // 算法开始
    qint64 m_algorithmEndTime = 0;  // 算法结束


    bool enableLocalMode;

    Detected_Data m_data;
    T_BatchLine m_Batchline;
    SqliteUtil m_sql;

    int m_nBatchId = 0;
};

#endif // MAINFORM_H
